package com.samsungmobile.common_module.SMS.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import com.ipx.www.api.services.smsapi51.types.SendResponse;
import com.pushmsg.OmaOtaConf.OmaOta;
import com.pushmsg.OmaOtaConf.OmaOtaBrowser;
import com.pushmsg.OmaOtaConf.OmaOtaMms;
import com.pushmsg.wappush.WapPush;
import com.samsungmobile.common_module.SMS.MessageBean;
import com.samsungmobile.common_module.SMS.RequestBean;

public abstract class OtaService {

	protected String serviceName = "";
	
	
	protected abstract SendResponse[] sendText(RequestBean rb, MessageBean mb) throws Exception;
	
	/**
	 * 
	 * @param rb
	 * @param mb
	 */
	protected abstract void setProperties(RequestBean rb, MessageBean mb);
	
	/**
	 * Sends a SMS to IPX and returns the IPX response
	 * @param mb
	 * @return
	 * @throws Exception
	 */
	protected abstract SendResponse[] invoke(MessageBean mb) throws Exception ;
	
	public SendResponse[] sendMessage(RequestBean rb) throws Exception {
		
		MessageBean mb = new MessageBean();
		SendResponse[] soapResponse = null;
		setProperties(rb, mb);

		if ( rb.getSmsType().equals("text") ){
			soapResponse = sendText(rb, mb);
			
		}else if( rb.getSmsType().equals("wapsetting") ){
			soapResponse = sendOmaWapSetting(rb, mb);

		}else if(  rb.getSmsType().equals("mmssetting") ){
			soapResponse = sendOmaMmsSetting(rb, mb);

		}else if( rb.getSmsType().equals("wappush") ){
			soapResponse = sendWapPush(rb, mb);
			
		}
		
		return soapResponse;
	}
	
	protected SendResponse[] sendOmaWapSetting(RequestBean rb, MessageBean mb) throws Exception {
		
		Vector<Properties> vector = new Vector<Properties>();
		Vector<byte[]> vpacket = new Vector<byte[]>();

		OmaOta ota = new OmaOta(Integer.parseInt("07"));

		OmaOtaBrowser oob = (OmaOtaBrowser)ota.getBody();
		oob.setProfileName(rb.getOperator() + " WAP");
		oob.setHomeURLSuffix("");
		oob.setNapDef(mb.getCountryCode(), rb.getOperator(), "gprs", "http");
		
		ota.process();
		
		int iMessageSegmentCount = ota.getNumSegment();
		int iMessageHeadLength = ota.getHeadLength();
		
		for( int i=0; i < iMessageSegmentCount; i++ ){
		    byte[] packet = ota.getSegment(i);
		    vpacket.add(packet);
		    String strPacket = bytesToHex(packet);
		    String sBody = strPacket.substring(iMessageHeadLength*2);  
		    String sHeader = strPacket.substring(0,iMessageHeadLength*2);

		    Properties p = new Properties();
		    p.setProperty("BODY", sBody);
		    p.setProperty("HEADER", sHeader);
		    vector.add(p);
		}
		mb.setBinaryMessage(vpacket);
	    mb.setMessage(vector);
	    mb.setMessageCount(iMessageSegmentCount );
		
	    return invoke(mb);		
	}
	
	protected SendResponse[] sendOmaMmsSetting(RequestBean rb, MessageBean mb) throws Exception {
		
		Vector<Properties> vector = new Vector<Properties>();
		Vector<byte[]> vpacket = new Vector<byte[]>();
		
		OmaOta ota = new OmaOta(Integer.parseInt("09"));

		OmaOtaMms oom = (OmaOtaMms)ota.getBody();
		oom.setProfileName(rb.getOperator()+" MMS");
		oom.setNapDef(mb.getCountryCode(), rb.getOperator(), "gprs", "http");	
		
		ota.process();
		
		int iMessageSegmentCount = ota.getNumSegment();
		int iMessageHeadLength = ota.getHeadLength();
		
		for( int i=0; i < iMessageSegmentCount; i++ ){
		    byte[] packet = ota.getSegment(i);
		    vpacket.add(packet);
		    String strPacket = bytesToHex(packet);
		    String sBody = strPacket.substring(iMessageHeadLength*2);  
		    String sHeader = strPacket.substring(0,iMessageHeadLength*2);

		    Properties p = new Properties();
		    p.setProperty("BODY", sBody);
		    p.setProperty("HEADER", sHeader);
		    vector.add(p);
		}
		mb.setBinaryMessage(vpacket);
	    mb.setMessage(vector);
	    mb.setMessageCount(iMessageSegmentCount );
		
	    return invoke(mb);		
	}

	public SendResponse[] sendWapPush(RequestBean rb, MessageBean mb) throws Exception {
		
		Vector<Properties> vector = new Vector<Properties>();
		Vector<byte[]> vpacket = new Vector<byte[]>();
		
		WapPush wapPush = new WapPush(mb.getOa(), rb.getMessage(), rb.getUrl());
		wapPush.process();
		
		int iMessageSegmentCount = wapPush.getSegNum();
		int iMessageHeadLength = wapPush.getHeadLength();
		
		for( int i=0; i < iMessageSegmentCount; i++ ){
		    byte[] packet = wapPush.getSegment(i);
		    vpacket.add(packet);
		    String strPacket = bytesToHex(packet);
		    String sBody = strPacket.substring(iMessageHeadLength*2);  
		    String sHeader = strPacket.substring(0,iMessageHeadLength*2);

		    Properties p = new Properties();
		    p.setProperty("BODY", sBody);
		    p.setProperty("HEADER", sHeader);
		    vector.add(p);
		}
		mb.setBinaryMessage(vpacket);
	    mb.setMessage(vector);
	    mb.setMessageCount(iMessageSegmentCount );
		
	    return invoke(mb);
	}
	
	protected static String bytesToHex(byte[] data){
	   
		StringBuffer buf = new StringBuffer();

       for (int i = 0; i < data.length; i++){
           buf.append(byteToHex(data[i]).toUpperCase());
       }
       
       return (buf.toString());
   }


   protected static String byteToHex(byte b) {

	   char hexDigit[] = {
           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
           'a', 'b', 'c', 'd', 'e', 'f'
       };
       
	   char array[] = {
           hexDigit[b >> 4 & 0xf], hexDigit[b & 0xf]
       };
       
	   return new String(array);
   }

   /*
    * Nokia OTA setting is not compleated now.
	public static void sendWapSetting(MessageBean mb){
		
		Vector<Properties> vector = new Vector<Properties>();
		
		String fnsuffix = getOtaFileName("t-mobile", "01");

		OtaConf ota = new OtaConf("D:\\02_Source\\samsungmobile\\prm\\conf\\"	+ "ger" + "\\" + fnsuffix, "", "setting");

		ota.process();
				
		int iMessageSegmentCount = ota.getNumSegment();
		int iMessageHeadLength = ota.getHeadLength();
		
		for( int i=0; i < iMessageSegmentCount; i++ ){
		    byte[] packet = ota.getSegment(i);
		    String strPacket = bytesToHex(packet);
		    String sBody = strPacket.substring(iMessageHeadLength*2);  
		    String sHeader = strPacket.substring(0,iMessageHeadLength*2);

		    Properties p = new Properties();
		    p.setProperty("BODY", sBody);
		    p.setProperty("HEADER", sHeader);
		    vector.add(p);
		}
		
	    mb.setMessage(vector);
	    mb.setMessageCount(iMessageSegmentCount );
		
		invoke(mb);		
	}
	
	public static void sendMmsSetting(){
		
	}	
	
	private static String getOtaFileName(String op, String type) {
		String fn = null;
		if (type.equals("00")) {
			fn = op + "_csd_wap.xml";
		} else if (type.equals("01")) {
			fn = op + "_gprs_wap.xml";
		} else if (type.equals("02")) {
			fn = op + "_internet.xml";
		} else if (type.equals("03")) {
			fn = op + "_bm.xml";
		} else if (type.equals("04")) {
			fn = op + "_mms.xml";
		} else if (type.equals("05")) {
			fn = op + "_syncml.xml";
		} else if (type.equals("06")) {
			fn = op + "_email.xml";
		}
		return fn;
	}   */
   
}
